const {
	Service
} = require("uni-cloud-router");
const {
	result_success,
	result_error
} = require('http-result-modle');
const db = uniCloud.database();
const dbCmd = db.command;
const $ = db.command.aggregate;
const uidObj = require('uni-id');
module.exports = class ChannelService extends(
	Service
) {

	async list(event) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		let params = event.data
		let skip = params.skip
		let limit = params.limit
		let type = params.type
		let qWhere = {}
		if (type === '调货大厅') {
			qWhere.type = 0
			qWhere.status = 0
		} else if (type == '换货') {
			qWhere.type = 1
			qWhere.status = 0
		} else if (type == '我的调货') {
			qWhere.create_uid = dbCmd.or({
				'create_uid': uid
			}, {
				'take_uid': uid
			})
			// qWhere.create_uid = uid
		} else {
			qWhere.status = 0
		}
		let data = await this.db.collection('wmzmall-goods-exchange').where(qWhere).get()
		return result_success(data.data);
	}
	async goods(event) {

		let start = Date.now();
		// 请求参数
		const req = this.ctx.data;
		let {
			name
		} = req;
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		if (!name) {
			return result_error('请输入商品名称搜索');
		}

		let whereObj = {
			state: '销售中'
		};
		whereObj.name = new RegExp(name, 'i')
		const goods = await this.db.collection('wmzmall-goods')
			.where(whereObj)
			.field({
				img: true,
				name: true,
				transfer_price: true,
				_id: true
			})
			.get();
		let data = goods.data
		let returnData = []
		for (var i in data) {
			let skus = await this.db.collection('wmzmall-goods-sku').where({
				goods_id: data[i]._id
			}).get()
			skus = skus.data
			for (var j in skus) {
				returnData.push({
					"title": data[i].name + "-" + skus[j].sku1_spec + "-192元",
					"sku": skus[j].sku1_spec,
					"name": data[i].name,
					"id": data[i]._id,
					"sku_id": skus[j]._id,
					"transfer_price": data[i].transfer_price,
					"count": 1,
					'img': data[i].img
				})
			}
		}
		return result_success(returnData)
	}
	async add(event) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		const cMemberRes = await this.db.collection('wmzmall-member').doc(uid).get()
		let app_id = cMemberRes.data[0].app_id
		let shop_id = cMemberRes.data[0].shop_id
		let params = event.data
		let type = params.type
		let goods = params.goods
		let remark = params.remark
		await this.db.collection('wmzmall-goods-exchange').add({
			type: type,
			goods: goods,
			is_delete: 0,
			status: 0,
			create_uid: uid,
			create_time: Date.now(),
			app_id: app_id,
			shop_id: shop_id,
			remark: remark
		})
		return result_success();
	}
	async cancel(event, context) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		let params = event.data
		let id = params.id
		let exchange = await this.db.collection('wmzmall-goods-exchange').doc(id).get()
		if (exchange.data.length === 0) {
			return result_error('id错误');
		}
		if (uid != exchange.data[0].create_uid) {
			return result_error('账号权限错误');
		}
		await this.db.collection('wmzmall-goods-exchange').doc(id).remove()
		return result_success();
	}
	async take(event, context) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		let params = event.data
		let id = params.id
		let exchange = await this.db.collection('wmzmall-goods-exchange').doc(id).get()
		if (exchange.data.length === 0) {
			return result_error('id错误');
		}
		await this.db.collection('wmzmall-goods-exchange').doc(id).update({
			take_uid: uid,
			status: 1
		})
		return result_success();
	}

	async cancelTakeOrder(event, context) {
		let uid = '';
		if (this.ctx.event.uniIdToken) {
			// 已登录，获取当前登录 uid
			const userRes = await uidObj.checkToken(this.ctx.event.uniIdToken);
			if (userRes.code == 0) {
				uid = userRes.uid;
			}
		}
		if (!uid) {
			return result_error('当前未登录');
		}
		let params = event.data
		let id = params.id
		let exchange = await this.db.collection('wmzmall-goods-exchange').doc(id).get()
		if (exchange.data.length === 0) {
			return result_error('id错误');
		}
		if (uid != exchange.data[0].take_uid) {
			return result_error('账号权限错误');
		}
		await this.db.collection('wmzmall-goods-exchange').doc(id).update({
			take_uid: '',
			status: 0
		})
		return result_success();
	}
};